<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * 
 * Generic debug formatting
 * 
 * 
 */
class X_Debug extends Exception
{
    /**
     * output using CLI
     *
     * @var bool
     */
    public static $bCli = false;
    /**
     * automatically print output when set to true
     *
     * @var bool
     */
    public static $bPrint = false;
    
    public static  function varDump($xObject)
    {
        self::out("<pre>".var_dump($xObject)."</pre>");
    }
    
    public static function out ($xObject)
    {
        static $sBGcolor = '#d4d4d4';
        $sBGcolor = ($sBGcolor == '#d4d4d4') ? '#c3d1df' : '#d4d4d4';
        static $sTGcolor = '#d1c6de';
        $sTGcolor = ($sTGcolor == '#d1c6de') ? '#c7c6de' : '#d1c6de';
        

        $aBacktrace = debug_backtrace();
        

        if (count($aBacktrace) > 1)
        {
            $aBacktrace = $aBacktrace[1];
            
            $aArgs = array();
            foreach ($aBacktrace['args'] as $xArgument)
            {
                
                switch (true)
                {
                    case is_object($xArgument):
                        $aArgs[] = get_class($xArgument);
                        break;
                    case is_scalar($xArgument):
                    default:
                        $aArgs[] = gettype($xArgument);
                        break;
                }
            }
            $sArgs = implode(', ', $aArgs);
            if (array_key_exists('function', $aBacktrace))
            {
                $sTemplate = (self::$bCli) ? '%5$s (line %6$s) <b>%1$s%2$s%3$s(%4$s)' : '%5$s (line %6$s) <b>%1$s%2$s%3$s(%4$s);</b>';
                if (array_key_exists('class' ,$aBacktrace))
                {
                	$sCalled = sprintf($sTemplate, @$aBacktrace['class'], @$aBacktrace['type'], @$aBacktrace['function'], $sArgs, @$aBacktrace['file'], @$aBacktrace['line']);
                }
                else
                {
                	$sCalled = 'main()';
                }
            }
        } else
        {
            $aBacktrace = array();
            $sCalled = 'main();';
        }
        
        if (self::$bCli)
        {
            $sString = "\n/*****DEBUG START*******/\n";
        }
        else
        {
            $sString = "<div style='padding:2px;background-color:{$sBGcolor};border: medium solid Gray'>";
            $sString .= "<div style='padding:2px;background-color:{$sTGcolor}'><small>debug from $sCalled</small></div>";
        }
        
        $aArguments = func_get_args();
        
        $sColor = '#efefef';
        
        $sColor = ($sColor == '#efefef') ? 'White' : '#efefef';
        $sStyle = sprintf('style="padding:4px;background-color:%1$s"', $sColor);
        
        if (count($aArguments) == 2 && is_string($aArguments[1]) && ! is_string($aArguments[0]))
        {
            array_unshift($aArguments, '<b>' . X_Array::popKey($aArguments, 1) . '</b>');
        }
        
        foreach ($aArguments as $xArgument)
        {
            $sColor = ($sColor == '#efefef') ? 'White' : '#efefef';
            $sStyle = sprintf('style="padding:4px;background-color:%1$s"', $sColor);
            
            if (self::$bCli)
            {
                switch (true)
                {
                    case (is_string($xArgument)):
                        $sVar = $xArgument;
                        break;
                    case (get_class($xArgument) == 'VARIANT'):
                        ob_start();
                        com_print_typeinfo($xArgument);
                        $sVar = ob_get_flush();
                        break;
                    default:
                        $sVar = print_r($xArgument, true);
                }
                
                $sString .= sprintf('%1$s'."\n", $sVar, $sStyle);
            }
            else if (is_string($xArgument))
            {
                $sString .= sprintf('<div %2$s>%1$s</div>', $xArgument, $sStyle);
            }
            else if (is_object($xArgument) && get_class($xArgument) == 'VARIANT')
            {
                ob_start();
                com_print_typeinfo($xArgument);
                $sVar = ob_get_flush();
                $sString = sprintf('<div %2$s><pre>%1$s</pre></div>', $sVar, $sStyle); 
            }
            else
            {
                $sString .= sprintf('<div %2$s><pre>%1$s</pre></div>', htmlentities(print_r($xArgument, true)), $sStyle);
            }
        }
        
        if (self::$bCli)
        {
            $sString .= "\n/*****DEBUG END*******/\n";
        }
        else
        {
            $sString .= "</div>";
        }
        
        return $sString;
    
    }
    
    public static function formatHtml ()
    {
        $aArguments = func_get_args();
        return call_user_func_array(array('X_Debug', 'out'), $aArguments);
    }
}